/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gen;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;

/**
 *
 * @author rodrigo
 */
public class Request {


    public InputStream request(String urlString,String[][] parametros)throws Exception {


        System.setProperty("http.proxyHost", "192.168.4.1");
        System.setProperty("http.proxyPort", "8080");



        // os parametros a serem enviados

        Properties parameters = new Properties();

        for(int i = 0;i < parametros.length; i++){
            parameters.setProperty(parametros[i][0], parametros[i][1]);
        }
        // o iterador, para criar a URL
        Iterator i = parameters.keySet().iterator();
        // o contador
        int counter = 0;
        // enquanto ainda existir parametros
        while (i.hasNext()) {
            // pega o nome
            String name = (String) i.next();
            // pega o valor
            String value = parameters.getProperty(name);
            // adiciona com um conector (? ou &)
            // o primeiro é ?, depois são &
            urlString += (++counter == 1 ? "?" : "&") + name + "=" + value;
        }
        // cria o objeto url
        URL url = new URL(urlString);
        // cria o objeto httpurlconnection
        HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();
        // seta o metodo
        connection.setRequestProperty("Request-Method", "GET");
        // seta a variavel para ler o resultado
        connection.setDoInput(true);
        connection.setDoOutput(false);
        connection.connect();
        // abre a conexão pra input

        return connection.getInputStream();
      /*  BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        // le ate o final
        StringBuffer newData = new StringBuffer(10000);
        String s = "";
        while (null != ((s = br.readLine()))) {
            newData.append(s);
        }
        br.close();
        // imprime o codigo resultante
        System.out.println(new String(newData)); */
        
    }

}
